package com.topfinance.frame.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.topfinance.frame.exception.AppException;
import com.opensymphony.xwork2.ActionSupport;

public class BaseSupport extends ActionSupport implements ServletRequestAware,
		SessionAware, ServletResponseAware {

	protected static Log log = LogFactory.getLog(BaseSupport.class);
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map sessionMap;
	protected List dataList;
	protected Map rtnResultMap;
	protected String start; // 为分页用，起始的记录数
	protected String limit; // 为分页用，返回多少条
	protected static final String LIST = "list";
	protected static final String CREATE = "create";
	protected static final String UPDATE = "update";
	protected static final String DELETE = "delete";
	protected static final String EDIT = "edit";
	protected static final String SAVE_ALL = "edit";
	protected static final String EDIT_DATA_ROOT = "editDataR";
	protected static final String DATA_LIST_ROOT = "dataListR";
	protected static final String RECORDS_COUNT = "recordsCount";
	protected static final String CREATE_SUCCESS_KEY = "create.success";
	protected static final String UPDATE_SUCCESS_KEY = "update.success";
	protected static final String DELETE_SUCCESS_KEY = "delete.success";
	protected static final String SAVE_SUCCESS_KEY = "save.success";
	protected Integer pageNum = 1; // 当前页数
	protected Integer countitem = 0; // 总记录数
	protected Integer numPerPage = 10; // 每页显示的数据条数
	protected Integer countpages = 0; // 总共的页数
	protected Integer start1 = 0; // 开始页
	protected Integer limit1;
	protected Integer currentPage = 1;

	public BaseSupport() {
		dataList = new ArrayList();
		rtnResultMap = null;
	}

	protected void handleException(Exception e) {
		log.error("", e);
		e.printStackTrace();
		rtnResultMap = new HashMap();
		if (e instanceof AppException) {
			AppException ae = (AppException) e;
			rtnResultMap.put("success", "false");
			String msg = getText(ae.getErrorCode());
			// if(ae.getParams() != null)
			// msg = MessageFormat.format(msg, ae.getParams());
			// if(ae.getCause() != null)
			// msg = (new
			// StringBuilder(String.valueOf(msg))).append(ae.getCause().getMessage()).toString();
			rtnResultMap.put("info", msg);
		} else {
			rtnResultMap.put("success", "false");
			rtnResultMap.put("info", getText("err.system.error"));
		}
	}

	public Map getRtnResultMap() {
		return rtnResultMap;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	protected PageReq getPageReq() {
		int startInt = 0;
		int limitInt = 0;
		int pageNum = PageReq.DEFAULT_PAGE_NUM;
		int pageSize = PageReq.DEFAULT_PAGE_SIZE;
		if (start != null) {
			try {
				startInt = Integer.parseInt(start);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (limit != null) {
			try {
				limitInt = Integer.parseInt(limit);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pageNum = (int) (startInt / limitInt) + 1;
		pageSize = limitInt;
		return new PageReq(pageNum, pageSize);

	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setSession(Map sessionMap) {
		this.sessionMap = sessionMap;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getCountitem() {
		return countitem;
	}

	public void setCountitem(Integer countitem) {
		this.countitem = countitem;
	}

	public Integer getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(Integer numPerPage) {
		this.numPerPage = numPerPage;
	}

	public Integer getCountpages() {
		return countpages;
	}

	public void setCountpages(Integer countpages) {
		this.countpages = countpages;
	}

	public Integer getStart1() {
		return start1;
	}

	public void setStart1(Integer start1) {
		this.start1 = start1;
	}

	public Integer getLimit1() {
		return limit1;
	}

	public void setLimit1(Integer limit1) {
		this.limit1 = limit1;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

}